﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Citiport.Util.IPGeo;

/// <summary>
/// NOT USE NOW
/// </summary>
public class UserUtil
{

}

/// <summary>
/// Facade class for user
/// </summary>
public static class CitiportUser
{
    public static String Userno
    {
        get
        {
            HttpContext current = HttpContext.Current;
            if (current == null)
                return null;
            String userno = DataUtil.ObjetToString(current.Session["userno"]);
            if(userno == null)
                userno = DataUtil.ObjetToString(current.Session[Citiport.Core.Const.SESSION_USER_ID]);
            HttpRequest request = current.Request;
            HttpResponse response = current.Response;
            CookiesManager cookies = new CookiesManager(request, response);
            if(userno==null)
                userno = cookies.getCookieValue(Citiport.Core.Const.COOKIE_USER_ID);
            return userno;
        }
        set
        {
            HttpContext current = HttpContext.Current;
            current.Session[Citiport.Core.Const.SESSION_USER_ID] = value;
            HttpRequest request = current.Request;
            HttpResponse response = current.Response;
            CookiesManager cookies = new CookiesManager(request, response);
            cookies.setCookieUserId(value);
        }
    }

    public static int Language
    {
        get
        {
            DatabaseUtil dbUtil = new DatabaseUtil();
            return dbUtil.getLanguageNo(LangCode);
        }
        set
        {
            HttpContext current = HttpContext.Current;
            HttpRequest request = current.Request;
            HttpResponse response = current.Response;
            CookiesManager cookies = new CookiesManager(request, response);
            cookies.setCookieUserPrefLang(value);
        }
    }

    public static String LangCode
    {
        get
        {
            HttpContext current = HttpContext.Current;
            
            HttpRequest request = current.Request;
            HttpResponse response = current.Response;
            GeoLingManager glm = new GeoLingManager(request, response);
            String code = glm.getLang();
            return code;
        }
        set
        {
            HttpContext current = HttpContext.Current;
            HttpRequest request = current.Request;
            HttpResponse response = current.Response;
            CookiesManager cookies = new CookiesManager(request, response);
            cookies.setCookieUserPrefLangCode(value);
        }
    }

    public static String IP
    {
        get
        {
            HttpContext current = HttpContext.Current;
            return current.Request.ServerVariables["REMOTE_ADDR"];
        }
        //NO SETTER
    }

    public static String LocationByIP
    {
        get
        {
            HttpContext current = HttpContext.Current;
            CountryLookup clookup = new CountryLookup(current.Server.MapPath("App_Data\\GeoIP.dat"));
            //Due to the localhost, use a fake to test
            String local = clookup.lookupCountryName(IP);
            return local;
        }
        //NO SETTER
    }

    /// <summary>
    /// User logout, clean session and cookie
    /// </summary>
    public static void Logout()
    {
        HttpContext current = HttpContext.Current;
        current.Session[Citiport.Core.Const.SESSION_USER_ID] = null;
        current.Session[Citiport.Core.Const.SESSION_USER_LANG] = null;
        HttpRequest request = current.Request;
        HttpResponse response = current.Response;
        CookiesManager cookies = new CookiesManager(request, response);
        cookies.removeCookie(Citiport.Core.Const.COOKIE_USER_ID);
        cookies.removeCookie(Citiport.Core.Const.COOKIE_USER_LANG);
        cookies.removeCookie(Citiport.Core.Const.COOKIE_USER_ACCESSIBLE_LANG);
    }

    public static void TwitterIt(String update)
    {
        Yedda.Twitter mytwi = new Yedda.Twitter();
        String username = "your_account";
        String password = "your_password";
        string result = mytwi.UpdateAsJSON(username, password, update);
    }

    public static void TwitterIt(String username, String pwd, String update)
    {
        Yedda.Twitter mytwi = new Yedda.Twitter();
        if (!DataUtil.IsEmptyString(username) && !DataUtil.IsEmptyString(pwd))
        {
            string result = mytwi.UpdateAsJSON(username, pwd, update);
        }
    }

}
